var Bmob = require("../../utils/bmob.js");
var common = require("../../utils/common.js");
const __utils = require('../../utils/util')
var app = getApp();
var that;
Page({
  data: {
    remind: '加载中',
    angle: 0,
    userInfo: {},
    options:{}
  },
  onLoad:function(t){
    that = this;
    that.setData({ options:t});
    var value = wx.getStorageSync('user_openid')
    if (value) {
      //准备就绪,进入下一页
      that.navigateToNextPage();
    }
  },
  navigateToNextPage: function(){
    if (that.data.options.page == "detail") {
      wx.navigateTo({ url: "/pages/detail/detail?actid=" + that.data.options.actid + "&pubid=" + that.data.options.pubid });
      that.setData({ options: {}});
    } else if (that.data.options.page == "user") {
      wx.switchTab({ url: "/pages/user/user" });
    } else {
      wx.switchTab({ url: "/pages/index/index" });
    }
  },
  getUserInfoFun: function (r){
    var that = this;
    wx.setStorageSync("userInfo", r.detail.userInfo);
    //调用API从本地缓存中获取数据
    try {
      var value = wx.getStorageSync('user_openid')
      if (value) {
        //准备就绪,进入下一页
        that.navigateToNextPage();
      } else {
        console.log('执行login...')
        wx.login({
          success: function (res) {
            if (res.code) {
              Bmob.User.requestOpenId(res.code, {
                success: function (userData) {
                  var result = r.detail;
                  var userInfo = result.userInfo
                  var nickName = userInfo.nickName
                  var avatarUrl = userInfo.avatarUrl
                  var sex = userInfo.gender
                  Bmob.User.logIn(userData.openid, userData.openid, {
                    success: function (user) {
                      try {
                        wx.setStorageSync('user_openid', user.get('userData').openid)
                        wx.setStorageSync('user_id', user.id)
                        wx.setStorageSync('my_nick', user.get("nickname"))
                        wx.setStorageSync('my_username', user.get('userData').openid)
                        wx.setStorageSync('my_sex', user.get("sex"))
                        wx.setStorageSync('my_avatar', user.get("userPic"));
                        //准备就绪,进入下一页
                        that.navigateToNextPage();
                      } catch (e) {
                      }
                      console.log("userid:" + user.id + ",username:" + user.get('userData').openid + " 登录成功");
                    },
                    error: function (user, error) {
                      if (error.code == '101') {
                        var user = new Bmob.User();//开始注册用户
                        user.set('username', userData.openid);
                        user.set('password', userData.openid);
                        user.set("nickname", nickName);
                        user.set("userPic", avatarUrl);
                        user.set("userData", userData);
                        user.set('sex', sex);
                        user.set('feednum', 0);
                        that.signUp(user);
                      }
                    }
                  });
                },
                error: function (error) {
                  console.log("Error: " + error.code + " " + error.message);
                }
              });
            } else {
              console.log('获取用户登录态失败1！' + res.errMsg)
            }
          },
          complete: function (e) {
            console.log('获取用户登录态失败2！' + e)
          }
        });
      }
    } catch (e) {
      console.log("登陆失败")
    }
  },
  signUp: function (user) {
    var that = this;
    user.signUp(null, {
      success: function (result) {
        console.log('注册成功');
        user.set('signUpSuccess', true);
        try {//将返回的3rd_session存储到缓存中
          wx.setStorageSync('user_openid', user.get('userData').openid)
          wx.setStorageSync('user_id', user.id)
          wx.setStorageSync('my_nick', user.get("nickname"))
          wx.setStorageSync('my_username', user.get("username"))
          wx.setStorageSync('my_sex', user.get("sex"))
          wx.setStorageSync('my_avatar', user.get("userPic"));

          //准备就绪,进入下一页
          that.navigateToNextPage();
        } catch (e) {
        }
      },
      error: function (userData, error) {
        console.log("openid=" + userData);
        console.log(error)
      }
    });
  },
  onShow:function(){
    console.log('onLoad')
    var that = this
  },
  onReady: function(){
    var _this = this;
    setTimeout(function(){
      _this.setData({
        remind: ''
      });
    }, 1000);
    wx.onAccelerometerChange(function(res) {
      var angle = -(res.x*30).toFixed(1);
      if(angle>14){ angle=14; }
      else if(angle<-14){ angle=-14; }
      if(_this.data.angle !== angle){
        _this.setData({
          angle: angle
        });
      }
    });
  },
});